#region Copyright Notice
// 
// 
//  
//  Copyright (c) 2009-2011 Anindya Chatterjee
//  
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at
//  
//      http://www.apache.org/licenses/LICENSE-2.0
//  
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// 
// 
#endregion
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace AbstractClass.Runtime.Serialization
{
    internal static class Security
    {
        internal static string Decrypt(string strText, string password)
        {
            if (string.IsNullOrEmpty(password) || password.Length < 8)
                password += "*#4$%^.+";

            byte[] iv = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
            MemoryStream ms;
            CryptoStream cs;
            DESCryptoServiceProvider des = null;
            try
            {
                using (des = new DESCryptoServiceProvider())
                {
                    byte[] bKey = Encoding.UTF8.GetBytes(password.Substring(0, 8));
                    byte[] inputByteArray = Convert.FromBase64String(strText);
                    using (ms = new MemoryStream())
                    {
                        cs = new CryptoStream(ms, des.CreateDecryptor(bKey, iv), CryptoStreamMode.Write);
                        cs.Write(inputByteArray, 0, inputByteArray.Length);
                        cs.FlushFinalBlock();
                        Encoding encoding = Encoding.UTF8;
                        string strResult = encoding.GetString(ms.ToArray());
                        return strResult;
                    }
                }
            }
            catch (Exception)
            {
                if (des != null) des.Clear();
                if (string.IsNullOrEmpty(strText))
                    return "";
                throw;
            }
        }

        internal static string Encrypt(string strText, string password)
        {
            if (string.IsNullOrEmpty(password) || password.Length < 8)
                password += "*#4$%^.+";

            byte[] dv = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
            MemoryStream ms;
            CryptoStream cs;
            DESCryptoServiceProvider des = null;

            try
            {
                byte[] byKey = Encoding.UTF8.GetBytes(password.Substring(0, 8));
                using (des = new DESCryptoServiceProvider())
                {
                    byte[] inputArray = Encoding.UTF8.GetBytes(strText);
                    using (ms = new MemoryStream())
                    {
                        cs = new CryptoStream(ms, des.CreateEncryptor(byKey, dv), CryptoStreamMode.Write);
                        cs.Write(inputArray, 0, inputArray.Length);
                        cs.FlushFinalBlock();
                        return Convert.ToBase64String(ms.ToArray());
                    }
                }
            }
            catch (Exception)
            {
                if (des != null) des.Clear();
                if (string.IsNullOrEmpty(strText))
                    return "";
                throw;
            }
        }

        internal static Stream Encrypt(Stream stream, string password)
        {
            if (string.IsNullOrEmpty(password) || password.Length < 8)
                password += "*#4$%^.+";

            byte[] dv = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
            DESCryptoServiceProvider des = null;

            try
            {
                byte[] byKey = Encoding.UTF8.GetBytes(password.Substring(0, 8));
                using (des = new DESCryptoServiceProvider())
                {
                    var cs = new CryptoStream(stream, des.CreateEncryptor(byKey, dv), CryptoStreamMode.Write);
                    return cs;
                }
            }
            catch (Exception)
            {
                if (des != null) des.Clear();
                throw;
            }
        }

        internal static Stream Decrypt(Stream stream, string password)
        {
            if (string.IsNullOrEmpty(password) || password.Length < 8)
                password += "*#4$%^.+";

            byte[] dv = {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF};
            DESCryptoServiceProvider des = null;

            try
            {
                byte[] byKey = Encoding.UTF8.GetBytes(password.Substring(0, 8));
                using (des = new DESCryptoServiceProvider())
                {
                    var cs = new CryptoStream(stream, des.CreateDecryptor(byKey, dv), CryptoStreamMode.Read);
                    return cs;
                }
            }
            catch (Exception)
            {
                if (des != null) des.Clear();
                throw;
            }
        }
    }
}